package com.sservice.service.service.impl;

import com.sservice.commonBusiness.e.Category;
import com.sservice.model.dao.IOfferDao;
import com.sservice.model.entity.Offer;
import com.sservice.model.vo.OfferVo;
import com.sservice.service.service.IOfferService;
import com.sservice.base.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class OfferServiceImpl implements IOfferService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IOfferDao dataDao;

    @Override
    public List<OfferVo> queryByPage(OfferVo user) {
        return dataDao.queryByPage(user);
    }

    @Override
    public List<OfferVo> selectByTheme(OfferVo record) {
        return dataDao.selectByTheme(record);
    }

    /**
     * @param record
     * @return
     */
    @Override
    public Map getCategoryByRcecord(OfferVo record) {
        List<Offer> list = dataDao.getCategoryIds(record);
        Map map = new HashMap();
        map.put("discount", getCategoryByDiscount(list)); //折扣
        map.put("age", getCategoryByAge(list)); //适用年龄
        map.put("color", getCategoryByColor(list)); //颜色
        map.put("season", getCategoryBySeason(list)); //季节
        map.put("categoryId", getCategoryByAge(list)); // 分类
        map.put("brand", getCategoryByAge(list));// 品牌
        map.put("applyGender", getGenderList()); // 性别
        map.put("sizeId", getCategoryBySize(list));
        return map;

    }


    public List getGenderList() {
        List list1 = new ArrayList();
        Map id = new HashMap();
        id.put("id", 0);
        id.put("name", "女性");
        list1.add(id);
        Map id1 = new HashMap();
        id1.put("id", 1);
        id1.put("name", "男性");
        list1.add(id1);

        Map id2 = new HashMap();
        id2.put("id", 2);
        id2.put("name", "中性");
        list1.add(id2);
        return list1;
    }

    public List<Category> getCategoryBySize(List<Offer> list) {
        List<String> ids = new ArrayList<>();
        list.stream()
                .filter(p -> !StringUtils.isBlank(p.getSizeId()))
                .forEach(p -> ids.add(p.getSizeId()));
        List<String> discountIds = ids.stream()
                .flatMap(item -> Stream.of(item.split(",")))
                .distinct()
                .collect(Collectors.toList());
        if (discountIds.size() == 0) {
            return new ArrayList();
        }
        return dataDao.getCategorys(discountIds);
    }

    public List<Category> getCategoryBySeason(List<Offer> list) {
        List<String> ids = new ArrayList<>();
        list.stream()
                .filter(p -> !StringUtils.isBlank(p.getSeason()))
                .forEach(p -> ids.add(p.getSeason()));
        List<String> discountIds = ids.stream()
                .flatMap(item -> Stream.of(item.split(",")))
                .distinct()
                .collect(Collectors.toList());
        if (discountIds.size() == 0) {
            return new ArrayList();
        }
        return dataDao.getCategorys(discountIds);
    }

    public List<Category> getCategoryByColor(List<Offer> list) {
        List<String> ids = new ArrayList<>();
        list.stream()
                .filter(p -> !StringUtils.isBlank(p.getColor()))
                .forEach(p -> ids.add(p.getColor()));
        List<String> discountIds = ids.stream()
                .flatMap(item -> Stream.of(item.split(",")))
                .distinct()
                .collect(Collectors.toList());
        if (discountIds.size() == 0) {
            return new ArrayList();
        }
        return dataDao.getCategorys(discountIds);
    }

    public List<Category> getCategoryByAge(List<Offer> list) {
        List<String> ids = new ArrayList<>();
        list.stream()
                .filter(p -> !StringUtils.isBlank(p.getApplyAge()))
                .forEach(p -> ids.add(p.getApplyAge()));
        List<String> discountIds = ids.stream()
                .flatMap(item -> Stream.of(item.split(",")))
                .distinct()
                .collect(Collectors.toList());
        if (discountIds.size() == 0) {
            return new ArrayList();
        }
        return dataDao.getCategorys(discountIds);
    }

    public List<Category> getCategoryByDiscount(List<Offer> list) {
        List<String> ids = new ArrayList<>();
        list.stream()
                .filter(p -> !StringUtils.isBlank(p.getDiscountId()))
                .forEach(p -> ids.add(p.getDiscountId()));
        List<String> discountIds = ids.stream()
                .flatMap(item -> Stream.of(item.split(",")))
                .distinct()
                .collect(Collectors.toList());
        if (discountIds.size() == 0) {
            return new ArrayList();
        }
        return dataDao.getCategorys(discountIds);
    }

    @Override
    public int countByTheme(OfferVo record) {
        return dataDao.countByTheme(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dataDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Offer record) {
        return dataDao.insert(record);
    }

    @Override
    public int insertSelective(Offer record) {
        return dataDao.insertSelective(record);
    }

    @Override
    public OfferVo selectByPrimaryKey(Integer id) {
        return dataDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Offer record) {
        return dataDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int queryOfferByGoodsId(String goodsId) {
        return dataDao.queryOfferByGoodsId(goodsId);
    }

    @Override
    public List<OfferVo> queryByPagePC(OfferVo user) {
        return dataDao.queryByPagePC(user);
    }

    @Override
    public int queryCountPC(OfferVo user) {
        return dataDao.queryCountPC(user);
    }

    @Override
    public List<OfferVo> queryByPagePCFocus(OfferVo user) {
        return dataDao.queryByPagePCFocus(user);
    }

    @Override
    public int queryCountPCFocus(OfferVo user) {
        return dataDao.queryCountPCFocus(user);
    }

    @Override
    public int queryCountByGoodsId(Offer user) {
        return dataDao.queryCountByGoodsId(user);
    }

    @Override
    public int queryCountMyOfferByCreatorId(Offer user) {
        return dataDao.queryCountMyOfferByCreatorId(user);
    }

    @Override
    public int selectByDeliverPlaceIds(Offer offer) {
        return dataDao.selectByDeliverPlaceIds(offer);
    }

    @Override
    public int queryCount(OfferVo user) {
        return dataDao.queryCount(user);
    }

    @Override
    public int countCommonByDr(OfferVo record) {
        return dataDao.countCommonByDr(record);
    }

    @Override
    public List<OfferVo> queryCommonByDr(OfferVo recode) {
        return dataDao.queryCommonByDr(recode);
    }
}
